package com.itheima.collecton.set.treeset;

import java.util.Comparator;
import java.util.TreeSet;

public class ComparatorDemo {
    public static void main(String[] args) {
        /*TreeSet<Teacher> ts = new TreeSet<>(new Comparator<Teacher>() {
            @Override
            public int compare(Teacher o1, Teacher o2) {
//                o1 表示现在想要存入的元素
//                o2 表示已经存入到集合中的元素
//                主要条件
                int result = o1.getAge() - o2.getAge();
//                次要条件
                result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
                return result;
            }
        });*/

        TreeSet<Teacher> ts = new TreeSet<>(
                (o1, o2) -> {
                    int result = o1.getAge() - o2.getAge();
                    result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
                    return result;
                });

        Teacher t1 = new Teacher("zhangsan",21);
        Teacher t2 = new Teacher("zhangshan",21);
        Teacher t3 = new Teacher("lisi",20);
        Teacher t4 = new Teacher("wangwu",24);
        Teacher t5 = new Teacher("zhaoliu",24);

        ts.add(t1);
        ts.add(t2);
        ts.add(t3);
        ts.add(t4);
        ts.add(t5);

        System.out.println(ts);

    }
}

class Teacher{
    private String name;
    private int age;

    public Teacher() {
    }

    public Teacher(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
